package com.huawei.esdk.uc.sametime.uccallbackimpl;

import java.util.Vector;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.jna.Constants.PhoneJointStatusType;
import com.huawei.esdk.uc.jna.callback.PhoneJointEventCB;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.widgets.TipPopup;

public class PhoneJointEventCBImpl implements PhoneJointEventCB
{
    private static Logger logger = Logger
            .getLogger(PhoneJointEventCBImpl.class);

    private static PhoneJointEventCBImpl instance = new PhoneJointEventCBImpl();

    private Vector<PhoneJoinCallBackInterface> phoneJoinNotifyQueue;

    private String statusTip = null;

    public static PhoneJointEventCBImpl getInstance()
    {
        return instance;
    }

    private PhoneJointEventCBImpl()
    {
        phoneJoinNotifyQueue = new Vector<PhoneJoinCallBackInterface>();
    }

    public void addObserver(PhoneJoinCallBackInterface observer)
    {
        phoneJoinNotifyQueue.add(observer);
    }

    public boolean removeObserver(PhoneJoinCallBackInterface observer)
    {
        return phoneJoinNotifyQueue.remove(observer);
    }

    @Override
    public void callBack(int state)
    {
        logger.info("phone join event callback, state is: " + state);

        switch (state)
        {
            case PhoneJointStatusType.STATUS_START_SUCC:

                statusTip = Messages.PhoneJoinEvent_START_SUCC;
                break;
            case PhoneJointStatusType.STATUS_START_FAILED:

                statusTip = Messages.PhoneJoinEvent_START_FAILED;
                break;
            case PhoneJointStatusType.STATUS_STOP_SUCC:

                statusTip = Messages.PhoneJoinEvent_STOP_SUCC;
                break;
            case PhoneJointStatusType.STATUS_STOP_FAILED:

                statusTip = Messages.PhoneJoinEvent_STOP_FAILED;
                break;
            case PhoneJointStatusType.STATUS_ONLINE:

                statusTip = Messages.PhoneJoinEvent_ONLINE;
                break;
            case PhoneJointStatusType.STATUS_OFFLINE:

                statusTip = Messages.PhoneJoinEvent_OFFLINE;
                break;
            case PhoneJointStatusType.STATUS_OFFHOOK:

                statusTip = Messages.PhoneJoinEvent_OFFHOOK;
                break;

            default:
                statusTip = null;
                break;
        }
        if (null != statusTip)
        {
            Display.getDefault().asyncExec(new Runnable()
            {
                @Override
                public void run()
                {
                    new TipPopup().showTipPopup(statusTip);
                }
            });
        }

        for (PhoneJoinCallBackInterface observer : phoneJoinNotifyQueue)
        {
            observer.handlePhoneJoin(state);
        }
    }

}
